// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Cork Activation Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.propertyValue : object.corkCount

private alias object.value0 : object.endSlot
private alias object.value1 : object.startSlot


// ========================
// Events
// ========================

event ObjectUpdate
	arrayPos0 = object.startSlot
	while arrayPos0 < object.endSlot
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++
	loop
end event


event ObjectDraw
	// Even if it's a "Draw" event, there's not really much drawing that actually happens here
	// Instead, it just does some more managing of priorities
	// (Keep in mind, when the player's dead Updates aren't ran anymore but Draws are)
	
	arrayPos0 = object.startSlot
	while arrayPos0 < object.endSlot
		if stage.state == STAGE_RUNNING
			object[arrayPos0].priority = PRIORITY_INACTIVE
		else
			object[arrayPos0].priority = PRIORITY_ACTIVE
		end if
		arrayPos0++
	loop
end event


event ObjectStartup
	foreach (TypeName[Cork Activation], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_XBOUNDS
		arrayPos1 = arrayPos0
		object[arrayPos0].endSlot = object[arrayPos0].corkCount

		temp0 = false
		while temp0 == false
			if object[arrayPos1].type == TypeName[Cork]
				object[arrayPos0].startSlot = arrayPos1
				temp0 = true
			else
				arrayPos1++
				if arrayPos1 >= 0x420
					temp0 = true
				end if
			end if
		loop
		object[arrayPos0].endSlot += object[arrayPos0].startSlot
	next
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
	
	if editor.showGizmos == true
		editor.drawingOverlay = true
		
		arrayPos0 = object.entityPos
		
		GetObjectType(temp0, "Cork")
		
		checkResult = true
		while checkResult == true
			checkNotEqual(object[arrayPos0].type, temp0)
			arrayPos0 += checkResult
			
			if arrayPos0 >= 0x420
				checkResult = false
			end if
		loop
		
		arrayPos1 = arrayPos0
		arrayPos1 += object.corkCount
		
		while arrayPos0 < arrayPos1
			DrawLine(object.xpos, object.ypos, object[arrayPos0].xpos, object[arrayPos0].ypos, 0xFF, 0xFF, 0x00)
			arrayPos0++
		loop
		
		editor.drawingOverlay = false
	end if
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-8, -8, 16, 16, 239, 239) // "Trigger" - #0
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "corkCount")
end event
